package first;
import java.io.File;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class CreatXMLFile 
{
	private ArrayList<Tweet> tweets;
	private File file;
	
	public static void main(String[] args) throws Exception 
	{
		
	}
	
	public void generator(ArrayList<Tweet> tweets2) throws ParserConfigurationException, TransformerException
    {
    	this.tweets = tweets2;
    	this.file = new File("result.xml");
    	
		DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
		Document document = documentBuilder.newDocument();
		Element rootElement = document.createElement("Tweets");
		document.appendChild(rootElement);
		for (int i = 0; i < tweets.size(); i++)
		{
			String element = ("Tweet "+ i);
			Element em = document.createElement(element);
			em.setAttribute("User", tweets.get(i).getUser());
        	em.setAttribute("Text", tweets.get(i).getText());
        	em.setAttribute("Updated", tweets.get(i).getUpdated());
        	em.setAttribute("Language", tweets.get(i).getLanguage());
        	em.setAttribute("Location", tweets.get(i).getLocation());
        	em.setAttribute("Picture", tweets.get(i).getPicture());
        	em.setAttribute("Rating", tweets.get(i).getRatingString());
			rootElement.appendChild(em);
		}
		TransformerFactory transformerFactory = TransformerFactory.newInstance();
		Transformer transformer = transformerFactory.newTransformer();
		DOMSource source = new DOMSource(document);
		StreamResult result =  new StreamResult(System.out);
		transformer.transform(source, result);
	}
}